import Mock from "mockjs";

// 模拟用户数据
Mock.mock("/api/users", "get", {
  code: 200,
  message: "success",
  "data|10": [
    {
      "id|+1": 1,
      name: "@cname",
      "age|18-60": 1,
      email: "@email",
      address: "@county(true)",
      avatar: "@image('200x200', '@color', '@name')",
    },
  ],
});

// 定义不同角色的可访问路由
const roleRoutes = {
  admin: [
    {
      path: "/home",
      name: "home",
      meta: { title: "首页", icon: "home" },
    },
    {
      path: "/user",
      name: "user",
      meta: { title: "用户管理", icon: "user" },
      children: [
        {
          path: "/user/list",
          name: "userList",
          meta: { title: "用户列表", icon: "list" },
        },
        {
          path: "/user/add",
          name: "userAdd",
          meta: { title: "添加用户", icon: "add" },
        },
      ],
    },
    {
      path: "/product",
      name: "product",
      meta: { title: "产品管理", icon: "product" },
    },
    {
      path: "/order",
      name: "order",
      meta: { title: "订单管理", icon: "order" },
    },
  ],
  editor: [
    {
      path: "/home",
      name: "home",
      meta: { title: "首页", icon: "home" },
    },
    {
      path: "/product",
      name: "product",
      meta: { title: "产品管理", icon: "product" },
    },
  ],
  user: [
    {
      path: "/home",
      name: "home",
      meta: { title: "首页", icon: "home" },
    },
  ],
};

// 模拟登录接口
Mock.mock("/api/login", "post", (options) => {
  const { username, password } = JSON.parse(options.body);

  // 管理员账号
  if (username === "admin" && password === "123456") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        token: "mock-token-admin",
        userInfo: {
          id: 1,
          name: "管理员",
          role: "admin",
          avatar: "https://randomuser.me/api/portraits/men/32.jpg",
          routes: roleRoutes.admin, // 返回管理员可访问的路由
        },
      },
    };
  }
  // 编辑账号
  else if (username === "editor" && password === "123456") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        token: "mock-token-editor",
        userInfo: {
          id: 2,
          name: "编辑",
          role: "editor",
          avatar: "https://randomuser.me/api/portraits/women/44.jpg",
          routes: roleRoutes.editor, // 返回编辑可访问的路由
        },
      },
    };
  }
  // 普通用户账号
  else if (username === "user" && password === "123456") {
    return {
      code: 200,
      message: "登录成功",
      data: {
        token: "mock-token-user",
        userInfo: {
          id: 3,
          name: "用户",
          role: "user",
          avatar: "https://randomuser.me/api/portraits/men/67.jpg",
          routes: roleRoutes.user, // 返回普通用户可访问的路由
        },
      },
    };
  }
  // 账号密码错误
  else {
    return {
      code: 401,
      message: "用户名或密码错误",
    };
  }
});

// 导出Mock对象
export default Mock;
